define(['./controllers','ducalendar'], function (controllers,ducalendar) {
    'use strict';
    controllers.controller('commitOrderCtrl', ['$scope', '$timeout', '$stateParams','lineServices','$compile','messageServices','$rootScope','orderServices','$state',
        function ($scope,$timeout, $stateParams,lineServices,$compile,messageServices,$rootScope,orderServices,$state) {
            //人数
            $scope.people = 1;
            //出游类型
            $scope.charge_type =0;
            //首页动画
            $scope.pageClass = 'page-home';
            //线路id
            $scope.lineid=$stateParams.lineid;
            //日历id
            $scope.calendarid = 0;
            //线路内容
            $scope.line;
            //联系人1
            $scope.linkman1 = {name:"zhaijg",tel:"135845545", passport:"111111222",card:"333424242"};
            //联系人2
            $scope.linkman2 = {name:"",tel:"", passport:"",card:""};
            //选择的当前日历
            $scope.activeCalendar = {};
            //日历对象
            var calendar ={};
            //激活日期
            var activeDate;
            //获取line详情
            lineServices.getLine($scope.lineid).then(function (lineData) {
                //console.log(lineData);
                $scope.line = lineData;
            });
            //获取当前line的日历列表
            lineServices.getCalendar($scope.lineid).then(function (lineCalendar) {
                $scope.line_calendar = lineCalendar;
                angular.forEach($scope.line_calendar, function (data, index, array) {
                    if(index==0){
                        $scope.activeCalendar = data;
                    }
                    var sdate = moment(data.sdate).format("YYYY-MM");
                    if(calendar[sdate]==undefined){
                        calendar[sdate]=[];
                    }
                    calendar[sdate].push(data);
                    if(sdate==moment().format("YYYY-MM")){
                        activeDate = sdate;
                    }else if(moment(sdate).isBefore(activeDate)){
                        activeDate = sdate;
                    }
                });
                for(var caTemp in calendar){
                    $('#data-tab').append('<a class="btn btn-product " id="d'+caTemp+'" ng-click="getcurDate(\''+moment(caTemp).format("YYYY")+'\',\''+moment(caTemp).format("MM")+'\')">'+moment(caTemp).format("YYYY")+'年'+moment(caTemp).format("MM")+'月</a>');
                }
                ////第一次时把日历调出
                $scope.getcurDate(moment(activeDate).format("YYYY"),moment(activeDate).format("MM"));
                $('#d' +activeDate).addClass('active');
                $scope.$evalAsync(function (){
                    $compile($("#data-tab"))($scope);
                })
                $scope.charge_type =2;
            });
            //显示指定月数据
            $scope.getcurDate =function (y, m) {
                $('#data-tab a').removeClass('active');
                $('#d' + y +"-"+ m).addClass('active');
                $timeout(function () {
                    $scope.$apply(function () {
                        ducalendar.showCalendar('#calendar', y, m, function () {
                            var caList = calendar[y + "-" + m];
                            for (var i = 0; i < caList.length; i++) {
                                var date = moment(caList[i].sdate).format("YYYY-MM-D");
                                $('[my-date="' + date + '"]').append('<div class="day-info">¥' + caList[i].full_payment + '</div><div class="day-info">剩余' + caList[i].inventory + '</div>');
                                $('[my-date="' + date + '"]').attr('ng-click', "selectDate(" + caList[i].id + ",\'"+date+"\')").css('cursor', 'pointer');
                            }
                        });
                        $scope.$evalAsync(function (){
                            $compile($("#calendar"))($scope);
                        });
                    });
                });
            }
            // 选项卡
            $('#table').find('a').on('click', function (e) {
                e.preventDefault();
                var _this = $(this);
                var index = _this.index();
                var $tabs = $('.tabs-box');
                _this.addClass('activity').siblings().removeClass('activity');
                $tabs.hide();
                $tabs.eq(index).show();
            });
            //选择日历
            $scope.selectDate = function (id,obj){
                $('[my-date]').removeClass('selectClass');
                $('[my-date="' + obj + '"]').addClass('selectClass');
                if($scope.calendarid != id){
                    $scope.calendarid = id;
                    for (var i = 0; i < $scope.line_calendar.length; i++) {
                        if ( $scope.calendarid == $scope.line_calendar[i].id) {
                            $scope.activeCalendar = $scope.line_calendar[i];
                            break;
                        }
                    }
                    computePrice();
                }
            }
            //提交订单
            $scope.submit_order = function () {
                if ( $scope.calendarid ==0) {
                    $("#calendar").focus();
                    messageServices.showMessage("出行日期没有选择");
                    return;
                }
                if($scope.linkman1.name==""){
                    $timeout(function(){
                        $("input[name='contact_name']").focus();
                    })
                    //$("input[name='contact_name']").focus();
                    messageServices.showMessage("联系人姓名没有输入");
                    return;
                }
                else if($scope.linkman1.tel==""){
                    messageServices.showMessage("联系人手机号没有输入");
                    return;
                }
                if($scope.people==2){
                    if($scope.linkman2.name==""){
                        messageServices.showMessage("同行人联系人姓名没有输入");
                        return;
                    }
                    else if($scope.linkman2.tel==""){
                        messageServices.showMessage("同行人联系人手机号没有输入");
                        return;
                    }
                }
                var list = [];
                var orderOne = {};
                var orderTwo = {};
                orderOne['sdate'] = $scope.activeCalendar.sdate;
                //订单状态:1 已取消 2 待确认 3待支付 4 已支付或首付已支付 5 已出团',
                orderOne['status'] = ($scope.process_mode==1?3:2);
                //订单类型 1主订单 2AA从属订单 3无需支付的从属订单',
                orderOne['order_type'] = 1;
                //日历id
                orderOne['line_calendar_id'] = $scope.activeCalendar.id;
                //线路名称
                orderOne['line_name'] = $scope.line.title;
                //渠道商
                orderOne['channel_name'] = $scope.line.channel;
                //出游人数
                orderOne['user_numbers'] = ($scope.people==1?1:2);
                //用户id
                orderOne['user_id'] = $rootScope.user.id;
                //实际出行用户姓名
                orderOne['user_name'] = $scope.linkman1.name;
                //实际出行用户电话
                orderOne['user_tel'] = $scope.linkman1.tel;
                //身份证号
                orderOne['card'] = $scope.linkman1.card;
                //护照号
                orderOne['passport'] = $scope.linkman1.passport;
                //购买方式: 1 全款支付 2 分期
                orderOne['charge_type'] = $scope.charge_type;
                list.push(orderOne);
                if($scope.people !=1){
                    orderTwo['sdate'] = $scope.activeCalendar.sdate;
                    //订单状态:1 已取消 2 待确认 3待支付 4 已支付或首付已支付 5 已出团',
                    orderTwo['status'] = ($scope.process_mode==1?3:2);;
                    //订单类型 1主订单 2AA从属订单 3无需支付的从属订单',
                    orderTwo['order_type'] = ($scope.people==2?3:2);
                    //日历id
                    orderTwo['line_calendar_id'] = $scope.activeCalendar.id;
                    //线路名称
                    orderTwo['line_name'] = $scope.line.title;
                    //渠道商
                    orderTwo['channel_name'] = $scope.line.channel;
                    //出游人数
                    orderTwo['user_numbers'] = 2;
                    //实际出行用户姓名
                    orderTwo['user_name'] = $scope.linkman2.name;
                    //实际出行用户电话
                    orderTwo['user_tel'] = $scope.linkman2.tel;
                    //身份证号
                    orderTwo['card'] = $scope.linkman2.card;
                    //护照号
                    orderTwo['passport'] = $scope.linkman2.passport;
                    //购买方式: 1 全款支付 2 分期
                    orderTwo['charge_type'] = $scope.charge_type;
                    list.push(orderTwo);
                }
                orderServices.putOrder(list).success(function (data) {
                    console.log(data);
                    $state.go('pay');
                });

            };
            $scope.show_down_payment=0;
            $scope.show_full_payment=0;
            //监听付款方式和人数
            $scope.$watchGroup(['charge_type', 'people'], function(newValue,oldValue, scope){
                computePrice();
            });
            /**
             * 计算价格
             */
            function computePrice() {
                //全款
                if ($scope.charge_type==1) {
                    //一个人补房价
                    if ($scope.people==1) {
                        $scope.show_full_payment = parseInt($scope.activeCalendar.full_payment)+parseInt($scope.activeCalendar.price_spread) ;
                    }else if ($scope.people==2) {
                        $scope.show_full_payment = parseInt($scope.activeCalendar.full_payment)*2 ;
                    }else{
                        $scope.show_full_payment = parseInt($scope.activeCalendar.full_payment);
                    }
                }else if( $scope.charge_type==2){
                    var price = parseInt($scope.activeCalendar.price_spread)/parseInt($scope.activeCalendar.down_payment_num);
                    //一个人补房价
                    if ($scope.people==1) {
                        $scope.show_down_payment = parseInt($scope.activeCalendar.down_payment)+parseInt(price) ;
                    }
                    //两个人付全款
                    else if ($scope.people==2) {
                        $scope.show_down_payment = parseInt($scope.activeCalendar.down_payment)*2 ;
                    }
                    //两个人aa
                    else{
                        $scope.show_down_payment = parseInt($scope.activeCalendar.down_payment);
                    }
                }
            }

        }
    ])

})
